package cn.zyx.pet.controller;

import cn.zyx.basic.exception.BusinessException;
import cn.zyx.basic.util.AjaxResult;
import cn.zyx.basic.util.LoginContext;
import cn.zyx.basic.util.PageList;
import cn.zyx.pet.service.IPetService;
import cn.zyx.pet.domain.Pet;
import cn.zyx.pet.domain.PetDetail;
import cn.zyx.pet.query.PetQuery;
import cn.zyx.pet.service.IPetService;
import cn.zyx.user.domain.LoginInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    // 添加或修改  @PutMapping(" ")
    // @RequestBody 前后端通过json传数据，数据放到请求体里面，请求体里面拿数据必须加上 --> restFul风格必须加上
    // 以前数据是放在请求头里面的，所以不用加，但是请求头的数据长度有限制
    @PutMapping
    @ApiOperation(value = "服务添加或修改", notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Pet pet) {
        try {
            if (pet.getId() == null) {
                petService.add(pet);
            } else {
                petService.update(pet);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请稍后再试!" + e.getMessage());
        }
    }

    // 查询一条数据
    // 不仅返回服务的信息，还返回服务详情的信息
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条服务信息或服务详情信息", notes = "需要传入id")
    public Pet getById(@PathVariable("id") Long id) {
        return petService.getById(id);
    }

    /**
     * 通过服务的id获取服务的详情
     *
     * @param petId
     * @return
     */
    @GetMapping("/detail/{petId}")
    @ApiOperation(value = "查询一条服务数据", notes = "需要传入petId")
    public PetDetail getDetailByPetId(@PathVariable("petId") Long petId) {
        return petService.getDetailByPetId(petId);
    }

    // 删除一条数据  @DeleteMapping(" ")
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除一条服务数据", notes = "需要传入id")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        // 链式编程 每一步操作都返回当前操作的对象
        try {
            petService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请稍后再试!" + e.getMessage());
        }
    }

    // 后端列表页查询接口 包含上架和下架 分页查询 @PostMapping(" ")
    @PostMapping
    @ApiOperation(value = "分页查询", notes = "需要传入高级查询需要的参数")
    public PageList<Pet> queryData(@RequestBody PetQuery petQuery) {
        return petService.queryPage(petQuery);
    }

    /**
     * 前端主站查询接口 -- 只传上架
     * @param petQuery
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "分页查询", notes = "需要传入高级查询需要的参数")
    public PageList<Pet> queryWebData(@RequestBody PetQuery petQuery) {
        petQuery.setState(1); // 默认上架
        return petService.queryPage(petQuery);
    }

    // 批量删除,如果前端传递的是数组，那么后端可以使用list或者array接收
    @PatchMapping
    @ApiOperation(value = "批量删除", notes = "需要传入ids")
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            petService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请稍后再试!" + e.getMessage());
        }
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有", notes = "查询所有")
    public List<Pet> getAll() {
        return petService.getAll();
    }

    // 批量上架
    @PostMapping("/onsale")
    @ApiOperation(value = "批量上架", notes = "需要传入ids")
    public AjaxResult onSale(@RequestBody List<Long> ids) {
        try {
            petService.onOrOffSale(ids, 1); // 1是上架 0是下架
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请稍后再试!" + e.getMessage());
        }
    }

    // 批量下架
    @PostMapping("/offsale")
    @ApiOperation(value = "批量下架", notes = "需要传入ids")
    public AjaxResult offSale(@RequestBody List<Long> ids) {
        try {
            petService.onOrOffSale(ids, 0); // 1是上架 0是下架
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请稍后再试!" + e.getMessage());
        }
    }

    // 领养宠物
    @GetMapping("/adopt/{petId}")
    @ApiOperation(value = "领养宠物", notes = "需要传入id")
    public AjaxResult adopt(@PathVariable("petId") Long petId, HttpServletRequest request) {
        try {
            // 拿到当前登录人的信息
            LoginInfo loginInfo = LoginContext.getLoginInfo(request);
            petService.adopt(petId, loginInfo.getId());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("领养失败!" + e.getMessage());
        }
    }

    /**
     * 领养宠物查看
     * @param request
     * @return
     */
    @PostMapping("/adopt/check")
    @ApiOperation(value = "领养宠物查看", notes = "需要传入高级查询需要的参数")
    public PageList<Pet> adoptPet(@RequestBody PetQuery petQuery,HttpServletRequest request) {
        // 拿到当前登录人的信息
        LoginInfo loginInfo = LoginContext.getLoginInfo(request);
        return petService.adoptPet(petQuery,loginInfo.getId());
    }
}